package com.kgc.springboot.hangduo.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kgc.springboot.hangduo.mapper.ClientMapper;
import com.kgc.springboot.hangduo.pojo.Client;
import com.kgc.springboot.hangduo.pojo.ClientExample;
import com.kgc.springboot.hangduo.pojo.User;
import com.kgc.springboot.hangduo.pojo.UserExample;
import com.kgc.springboot.hangduo.service.ClientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ClientServiceImpl implements ClientService {
    @Autowired
    private ClientMapper clientMapper;
    //查询分页
    @Override
    public PageInfo<Client> selectClientAll(String clientPhone, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        PageHelper.orderBy("id");
        ClientExample clientExample = new ClientExample();
        if (clientPhone!=null&&clientPhone.length()>0){
            clientExample.createCriteria().andClientPhoneLike("%"+clientPhone+"%");
        }
        List<Client> clients = clientMapper.selectByExample(clientExample);
        PageInfo<Client> pageInfo = new PageInfo<>(clients);
        return pageInfo;
    }

    @Override
    public Client login(String phone, String password) {
        ClientExample clientExample = new ClientExample();
        clientExample.createCriteria().andClientPhoneEqualTo(phone).andClientPasswordEqualTo(password);
        List<Client> clients = clientMapper.selectByExample(clientExample);
        if (clients.size()>0){
            Client client = clients.get(0);
            return client;
        }else {
            return null;
        }
    }

    //前端注册
    @Override
    public int insertSelective(Client client) {
        return clientMapper.insertSelective(client);
    }

    //验证手机号
    @Override
    public Client getPhone(String phone) {
        ClientExample clientExample = new ClientExample();
        clientExample.createCriteria().andClientPasswordEqualTo(phone);
        List<Client> clients = clientMapper.selectByExample(clientExample);
        if (clients.size()>0){
            Client client = clients.get(0);
            return client;
        }else {
            return null;
        }
    }

    @Override
    public Integer updateById(Client client) {
        int i = clientMapper.updateByPrimaryKeySelective(client);
        return i;
    }
}
